//
//  XMNewMobileView.m
//  XMJJ_zml
//
//  Created by 丁浩 on 2025/6/3.
//

#import "XMNewMobileView.h"

@interface XMNewMobileView ()

@property (nonatomic, strong) UITextField *mobileTextField;
@property (nonatomic, strong) UITextField *codeTextField;

@end

@implementation XMNewMobileView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    [self addSubview:self.navView];
    self.navView.backgroundColor = kColorHEX(0xffffff);
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(28.0), kNavagationBarH + KScaleWidth(24.0), self.width - KScaleWidth(28.0), KScaleWidth(46.0)) font:KFontSize(24.0) textColor:kColorHEX(0x221232)];
    titleLabel.text = @"请输入新手机号" ;
    [self addSubview:titleLabel];
    
    UILabel *explainLabel = [[UILabel alloc] initWithFrame:CGRectMake(titleLabel.x, titleLabel.maxY, titleLabel.width, KScaleWidth(46.0)) font:KFontSize(14.0) textColor:kColorHEX(0x988DA2)];
    explainLabel.text = @"换绑新手机号之后，可以用新的手机号及当前密码登录";
    [self addSubview:explainLabel];
    
    [self addSubview:self.mobileTextField];
    [self addSubview:self.codeTextField];
    _mobileTextField.y = explainLabel.maxY + KScaleWidth(62.0);
    _codeTextField.y = _mobileTextField.maxY + KScaleWidth(16.0);
    
    UIButton *commitBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KBoldFontSize(17.0) frame:CGRectMake((self.width - KScaleWidth(320.0))/2, _codeTextField.maxY + KScaleWidth(225.0), KScaleWidth(320.0), KScaleWidth(44.0)) title:@"提交" titleColor:kColorHEX(0x333333)];
    [commitBtn setGradientLayerWithLocations:@[@(0), @(1.0)] colors:@[(__bridge id)kColorHEX(0xFFD800).CGColor, (__bridge id)kColorHEX(0xFFE44E).CGColor] startPoint:CGPointMake(0.5, 0.37) endPoint:CGPointMake(0.03, .037)];
    [commitBtn addTarget:self action:@selector(commitAction:) forControlEvents:UIControlEventTouchUpInside];
    [commitBtn setCornerRadius:commitBtn.height/2];
    [self addSubview:commitBtn];
}

//获取验证码
- (void)getCodeAction:(UIButton *)button {
    if (!_mobileTextField.text || _mobileTextField.text.length == 0) {
        [MBProgressHUD showError:@"请输入手机号"];
        return;
    }
    self.codeSendBlock ? self.codeSendBlock(_mobileTextField.text) : nil;
}

//提交
- (void)commitAction:(UIButton *)button {
    if (!_mobileTextField.text || _mobileTextField.text.length == 0) {
        [MBProgressHUD showError:@"请输入手机号"];
        return;
    }
    if (!_codeTextField.text || _codeTextField.text.length == 0) {
        [MBProgressHUD showError:@"请输入验证码"];
        return;
    }
    self.commitBlock ? self.commitBlock(_mobileTextField.text, _codeTextField.text) : nil;
}

#pragma mark - Init
- (UITextField *)mobileTextField {
    if (!_mobileTextField) {
        _mobileTextField = [[UITextField alloc] initWithFrame:CGRectMake((self.width - KScaleWidth(320.0))/2, 0, KScaleWidth(320.0), KScaleWidth(44.0))];
        _mobileTextField.font = KFontSize(16.0);
        _mobileTextField.placeholder = @"请输入手机号码";
        _mobileTextField.backgroundColor = kColorHEX(0xF5F3F7);
        [_mobileTextField setCornerRadius:_mobileTextField.height/2];
        
        UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScaleWidth(84.0), _mobileTextField.height)];
        UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(16.0) frame:CGRectMake(0, 0, KScaleWidth(70.0), leftView.height) title:@"+86" titleColor:kColorHEX(0x221232) image:@""];
        [leftView addSubview:leftBtn];
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(KScaleWidth(71.0), (leftView.height - KScaleWidth(28.0))/2, 1, KScaleWidth(28.0))];
        lineView.backgroundColor = kColorHEX(0xCBC3D2);
        [leftView addSubview:lineView];
        _mobileTextField.leftViewMode = UITextFieldViewModeAlways;
        _mobileTextField.leftView = leftView;
    }
    return _mobileTextField;
}

- (UITextField *)codeTextField {
    if (!_codeTextField) {
        _codeTextField = [[UITextField alloc] initWithFrame:CGRectMake((self.width - KScaleWidth(320.0))/2, 0, KScaleWidth(320.0), KScaleWidth(44.0))];
        _codeTextField.font = KFontSize(16.0);
        _codeTextField.backgroundColor = kColorHEX(0xF5F3F7);
        _codeTextField.placeholder = @"请输入验证码";
        _codeTextField.keyboardType = UIKeyboardTypeNumberPad;
        [_codeTextField setCornerRadius:_codeTextField.height/2];
        
        UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScaleWidth(16.0), _codeTextField.height)];
        _codeTextField.leftViewMode = UITextFieldViewModeAlways;
        _codeTextField.leftView = leftView;
        
        UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScaleWidth(102.0), _codeTextField.height)];
        UIButton *getCodeBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(14.0) frame:rightView.bounds title:@"获取验证码 " titleColor:kColorHEX(0xDD4B39)];
        [getCodeBtn addTarget:self action:@selector(getCodeAction:) forControlEvents:UIControlEventTouchUpInside];
        [rightView addSubview:getCodeBtn];
        _codeTextField.rightViewMode = UITextFieldViewModeAlways;
        _codeTextField.rightView = rightView;
    }
    return _codeTextField;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
